import crypto from 'crypto'
export default class YzOperand {
  static supportType = [
    'variable',
    'op',
    'string',
    'real',
    'empty'
  ]
  constructor (type, opValue) {
    if (!YzOperand.supportType.includes(type)) {
      throw new Error('不支持的值类型')
    }
    this.type = type
    this.opValue = opValue
  }

  getDisplayString () {
    if (this.type === 'op') {
      return this.opValue.getDisplayString()
    }
    return this.opValue ?? ''
  }

  getExecJsString () {
    if (this.type === 'op') {
      return this.opValue.getExecJsString()
    } else if (this.type === 'variable') {
      let hash = crypto.createHash('md5')
      return `v_${hash.update(this.opValue).digest('HEX')}`
    } else if (this.type === 'string') {
      return `'${this.opValue}'`
    }
    return this.opValue ?? ''
  }
}
